package com.ng.framework.view.desktop;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JTextField;

import com.qt.datapicker.DatePicker;

public class NGDatePicker extends JTextField implements Observer, MouseListener {

	private static final long serialVersionUID = 5632025951938582127L;

	private String dateFormat = "dd/MM/yyyy";

	private Locale locale = new Locale("es", "AR");
	
	boolean centerOnScreen = false;

	public NGDatePicker() {
		this(false);
	}
	
	public NGDatePicker(boolean centerOnScreen) {
		this(centerOnScreen, 7);
	}
	
	public NGDatePicker(int columns) {
		this(false, columns);
	}
	
	public NGDatePicker(boolean centerOnScreen, int columns) {
		super(columns);
		
		this.centerOnScreen = centerOnScreen;
				
		this.addMouseListener(this);
	}

	@Override
	public void update(Observable o, Object arg) {
		Calendar calendar = (Calendar) arg;
		DatePicker dp = (DatePicker) o;
		this.setText(dp.formatDate(calendar, dateFormat));

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		DatePicker dp = new DatePicker(this, locale);
		// previously selected date
		Date selectedDate = dp.parseDate(this.getText());
		dp.setSelectedDate(selectedDate);
		dp.start(centerOnScreen ? null : this);

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {

	}

	@Override
	public void mouseExited(MouseEvent e) {

	}
}
